package edu.psu.ist131.lib;

import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.table.TableModel;
import javax.swing.tree.TreeModel;

public class FileDbSampleCode
{
    public static void main(String[] args) throws Exception
    {
        String dpath = "C:/PerfLogs";       // TODO: change this to a directory on your PC that you want to search
        FilenameFilter fltr = null;
        Collection<FileMetadata> d1 = FileMetadata.getFiles(dpath, fltr, true);         // go get all files from the directory
        System.out.println(d1.size());

        FileDb db = new FileDb();
        db.AddFilesToDb(d1);
        Collection<FileMetadata> iniFiles = db.Query("select from FileMetadata where extension = 'ini' order by size");     // TODO: change this to the kind of query you actually want
//        Collection<FileMetadata> iniFiles = db.Query("select from FileMetadata where name like '%.ini' order by size");   // query w/ same effect, written differently
        
        if(iniFiles.size() < 1 && db.GetCaughtException() != null)
        {
            System.out.println("ERROR: " + db.GetCaughtException().getMessage());
        }
        
        for(FileMetadata c : iniFiles)
        {
            System.out.println(c.getName() + ": " + c.getSize());
        }
        
        
        // Example of how to get a list of POSSIBLE duplicates:
        
        /* step 1: build a list of the fieldnames you want to use
         *      path    (the file's absolute path)
         *      hash    (the file's MD5)
         *      ext     (the file's extension)
         *      mod     (the file's last modified date)
         *      name    (the file's name, including extension)
         *      size    (the file's size in bytes)
         */ 
        ArrayList<String> fieldnames1 = new ArrayList<String>();       
        fieldnames1.add("hash");
        fieldnames1.add("size");
//        fieldnames1.add("ext");
//        fieldnames1.add("mod");
//        fieldnames1.add("name");
//        fieldnames1.add("path");
        
        /* step 2: call FileMetadata.getPossibleDuplicates with 
         *      
         */
        HashMap<String, Collection<FileMetadata>> dupes = FileMetadata.getPossibleDuplicates(d1, fieldnames1, true);

        for(String key : dupes.keySet())
        {
            System.out.println("Set of duplicates:");
            Collection<FileMetadata> c = dupes.get(key);
            for(FileMetadata file : c)
            {
                System.out.println(file.getName() + " at " + file.getAbsolutePath());
            }
            System.out.println();
        }

        // how to populate a JTable:
        ArrayList<String> fields = new ArrayList<String>();                         // define the columns to show
        fields.add("Path");
        fields.add("Name");
        fields.add("Size");
        fields.add("LastMod");
        Collection<FileMetadata> files = FileMetadata.getFiles(dpath, null, true);  // go get all files from the directory
        TableModel tm = FileMetadata.getTableModel(files, fields);                  // get it all back in a new TableModel
        JTable jTable1 = new JTable();                                              // you won't need this line -- NetBeans will create the object for you
        jTable1.setModel(tm);
        jTable1.setAutoCreateRowSorter(true);                                       // optional, but nice
        jTable1.setAutoResizeMode(jTable1.AUTO_RESIZE_ALL_COLUMNS);                 // optional, but nice
        
        
        // how to populate a JTree with duplicates
        Collection<FileMetadata> allFiles = FileMetadata.getFiles(dpath, null, true);   // go get all files from the directory
        ArrayList<String> fieldnames = new ArrayList<String>();                         // define the fields used for uniqueness
        fieldnames.add("hash");
        fieldnames.add("size");

        HashMap<String, Collection<FileMetadata>> dupes2 = FileMetadata.getPossibleDuplicates(allFiles, fieldnames, true);  // get your duplicates
        TreeModel tm2 = FileMetadata.getTreeModelOfDuplicates(dupes2);                  // get it all back in a new TreeModel
        JTree jTree1 = new JTree();                                                     // you won't need this line -- NetBeans will create the object for you
        jTree1.setModel(tm2);
        
        
        // how to populate a JTree with folders/files
        TreeModel tm3 = FileMetadata.getTreeModelOfDirectories(dpath, null, true, false);   // you can choose to include empty directories or not
        JTree jTree2 = new JTree();                                                         // you won't need this line -- NetBeans will create the object for you
        jTree2.setModel(tm3);
        
    }
    
}
